﻿import QtQuick 2.0
import "./components"


// 实验状态

Item {


    // 实验名称
    property string mainMethod: ""
    property string singleMethod_1: ""
    property string singleMethod_2: ""
    property string singleMethod_3: ""
    property string singleMethod_4: ""
    property string consumableName: ""
    property string consumableCode: ""
    // 耗材编码有效性
    property bool consumableCodeStatus: false   // 耗材编码状态
    property string consumableCodePromptMsg: ""    // 耗材编码错误时的提示信息

    property bool isConfirm_main: false
    property bool isConfirm_1: false
    property bool isConfirm_2: false
    property bool isConfirm_3: false
    property bool isConfirm_4: false

    // 每个步骤的状态
    property bool isUnion: false        // 是否在并发状态
    property var moduleStatus: [0, 0, 0, 0, 0]           // -1错误 0不执行 1等待执行 2运行中 3已完成
    property var moduleConnectStatus: [1, 1, 1, 1]          // -1错误 0灰的  2绿的 还有别的色吗……

    // 没啥用的信息
    property string expName: ""
    property string equName: ""
    property string execDate: ""
    property string operator: ""
    property string expCategories: ""
    property string memo: ""

    property int expRemainTime: 0
    property int expTotalTime: 0
    property int expRunningTime: 0
    property int expPauseTime: 0
    property int expWaitTime: 0
    property int expAirPumpTime: 0
    property int expWaterPumpTime: 0
    property int expStep: 0
    property int expStepCount: 0
    property int expPauseType: 0   // 用来区分暂停的类型是普通的暂停还是能谱图的暂停 0普通暂停 1能谱图家的暂停 2活检的暂停

    // 活检浓度（先塞这儿了）
    property string hoJianConc: "--"

    property int     selectedExpType: -1
    property string  selectedExpName: ""


//    property int

    // 运行时判断用户是否确认了

    function checkIsConfirm(){
        if(mainMethod !== ""){
            return isConfirm_main
        }else{
            if(gPageIdx === 1){
                return isConfirm_1
            }else if(gPageIdx === 2){
                return isConfirm_2
            }else if(gPageIdx === 3){
                return isConfirm_3
            }else if(gPageIdx === 4){
                return isConfirm_4
            }else{
                console.log("ERROR: ControlPannel-页面飞了")
                return false
            }
        }
    }

    // 启动实验时清空步骤状态
    function clearModuleStatus(){
        moduleStatus = [0, 0, 0, 0, 0]
        moduleConnectStatus = [0, 0, 0]
    }
    function initModuleStatus(){

        moduleStatus = [0, 0, 0, 0, 0]
        if(selectedExpType === 0){
            moduleStatus = [0, 1, 1, 1, 1]
        }else{
            moduleStatus[selectedExpType] = 1
        }

        moduleStatusChanged()

        moduleConnectStatus = [0, 0, 0]

    }


    function syncStateFromDB(){
        console.log("从数据库同步实验信息")
        var stateMap = CAnimationRescue.getRescueExpInfo()
        selectedExpType = stateMap["ExpType"]
        mainMethod = stateMap["MainMethod"]
        if(mainMethod === ""){
            singleMethod_1 = stateMap["SingleMethod1"]
            singleMethod_2 = stateMap["SingleMethod2"]
            singleMethod_3 = stateMap["SingleMethod3"]
            singleMethod_4 = stateMap["SingleMethod4"]
        }
        memo = stateMap["Memo"]
        operator = stateMap["Operator"]
        expCategories = stateMap["ExpType"]
        consumableCode = stateMap["ConsumableCode"]
        consumableName = stateMap["ConsumableName"]
    }

    // 点击确认
    CustomPopupBoxWarning{
        id: box_confirmExp
        box_text: "是否更换耗材【" + consumableName + "】？"
        onAccepted: {
//            confirmExp2()
            confirmConsumableName2()
        }
    }

    function confirmConsumableName1(){
        // 连接状态校验
        if(gConnectStatus === false){
            box_error_common.box_text = "请先连接设备"
            box_error_common.show()
            return
        }
        if(consumableName === ""){
            box_error_common.box_text = "请选择纯化耗材"
            box_error_common.show()
            return
        }
        box_confirmExp.show()
    }
    function confirmConsumableName2(){
        CGetMessageInterface.confirmClicked(consumableName)
    }

    // 用户点击确认，校验第一步
    function confirmExp(){

        // 连接状态校验
        if(gConnectStatus === false){
            box_error_common.box_text = "请先连接设备"
            box_error_common.show()
            return
        }
        // 是否选择了实验的校验
        if(gPageIdx === 0 && mainMethod === ""){
            box_error_common.box_text = "请选择正确的实验"
            box_error_common.show()
            return
        }else if(gPageIdx === 1 && singleMethod_1 === ""){
            box_error_common.box_text = "请选择正确的实验"
            box_error_common.show()
            return
        }else if(gPageIdx === 2 && singleMethod_2 === ""){
            box_error_common.box_text = "请选择正确的实验"
            box_error_common.show()
            return
        }else if(gPageIdx === 3 && singleMethod_3 === ""){
            box_error_common.box_text = "请选择正确的实验"
            box_error_common.show()
            return
        }else if(gPageIdx === 4 && singleMethod_4 === ""){
            box_error_common.box_text = "请选择正确的实验"
            box_error_common.show()
            return
        }
//        confirmExp = 0
        // 耗材编码校验
        var checkCodeRes = CBarCode.isValidCode(consumableCode)
        if(checkCodeRes !== "" && isCheckConsumableCode){
            if(mainMethod !== "" || gPageIdx===4){
                box_error_common.box_text = checkCodeRes
                box_error_common.show()
                if(gPageIdx !== 4){     // 跳转到主页提醒用户填写耗材编码
                    gPageIdx = 0
                }
                return
            }
        }


        // 用户确认
        if(mainMethod!==""){
            box_confirmExp.box_text = "是否选择项目【"+mainMethod+"】和耗材【"+consumableName+"】进行复位?"
        }else if(gPageIdx === 1){
            box_confirmExp.box_text = "是否选择实验【"+singleMethod_1+"】进行复位?"
        }else if(gPageIdx === 2){
            box_confirmExp.box_text = "是否选择实验【"+singleMethod_2+"】进行复位?"
        }else if(gPageIdx === 3){
            box_confirmExp.box_text = "是否选择实验【"+singleMethod_3+"】进行复位?"
        }else if(gPageIdx === 4){
            box_confirmExp.box_text = "是否选择实验【"+singleMethod_4+"】和耗材【"+consumableName+"】进行复位?"
        }
        box_confirmExp.show()
    }

    // 用户点击确认，校验第二步
    function confirmExp2(){

        CGetMessageInterface.confirmClicked()           // 调用确认

        // 主页 -----------------------------
        if(gPageIdx === 0){
            if(mainMethod !== ""){                                      // 主流程实验0
//                var checkRes = CGetMessageInterface.setExpInfo(0, mainMethod, consumableName)
//                if(checkRes !== ""){
//                    box_error_common.box_text = checkRes
//                    box_error_common.show()
//                    return
//                }
                isConfirm_main = true
            }
        }
        // 模块页 ----------------------------
        else{

            if(mainMethod !== ""){                                      // 主流程实验0
//                var checkRes = CGetMessageInterface.setExpInfo(0, mainMethod, consumableName)
//                if(checkRes !== ""){
//                    box_error_common.box_text = checkRes
//                    box_error_common.show()
//                    return
//                }
                isConfirm_main = true
                // 实验动画复位
                CAnimationShifting.reset()
                CAnimationPurify.reset()
                CAnimationPractice.reset()

            }
            else{
                // 单个流程实验
                if(gPageIdx === 1 && singleMethod_1 !== ""){            // 模块1
//                    var checkRes = CGetMessageInterface.setExpInfo(1, singleMethod_1)
//                    if(checkRes !== ""){
//                        box_error_common.box_text = checkRes
//                        box_error_common.show()
//                        return
//                    }
                    isConfirm_1 = true
                    CAnimationShifting.reset()  // 实验动画复位
                }else if(gPageIdx === 2 && singleMethod_2 !== ""){      // 模块2
//                    var checkRes = CGetMessageInterface.setExpInfo(2, singleMethod_2)
//                    if(checkRes !== ""){
//                        box_error_common.box_text = checkRes
//                        box_error_common.show()
//                        return
//                    }
                    isConfirm_2 = true
                    CAnimationPractice.reset()  // 实验动画复位
                }else if(gPageIdx === 3 && singleMethod_3 !== ""){      // 模块3
//                    var checkRes = CGetMessageInterface.setExpInfo(3, singleMethod_3)
//                    if(checkRes !== ""){
//                        box_error_common.box_text = checkRes
//                        box_error_common.show()
//                        return
//                    }
                    isConfirm_3 = true
                    CAnimationShifting.reset()  // 实验动画复位
                }else if(gPageIdx === 4 && singleMethod_4 !== ""){      // 模块4
//                    var checkRes = CGetMessageInterface.setExpInfo(4, singleMethod_4, consumableName)
//                    if(checkRes !== ""){
//                        box_error_common.box_text = checkRes
//                        box_error_common.show()
//                        return
//                    }
                    isConfirm_4 = true
                    CAnimationPurify.reset()    // 实验动画复位
                }else{
                    box_error_common.box_text = "请选择正确的实验"
                    box_error_common.show()
                    return
                }
            }

        }

        console.log("当前实验类型", selectedExpType)
        if(selectedExpType === 0 || selectedExpType === 4){
//            toast.show("实验流程下发成功，请等待设备复位", 0)
            box_resetting.show()        // 显示等待复位的弹窗
        }else{
            box_succeed_common.box_text = "确认实验成功"
            box_succeed_common.show()
        }

    }

    // 打包实验信息
    function getExpUserInfo(){

        var res = {}
        res["ExpName"] = expName
        res["EquName"] = equName
        res["Date"] = execDate
        res["Operator"] = operator
        res["ExpType"] = expCategories
        res["Memo"] = memo
        res["ConsumableCode"] = consumableCode
        res["ExpType"] = expCategories

        return res

    }

    // 初始化实验信息
    function initExpInfo(){
        expName = String("实验%1").arg(Qt.formatDateTime(new Date(), "yyyyMMddhhmmss"))
        equName = "EZ Studio V1.1"
        execDate = Qt.formatDateTime(new Date(), "yyyy-MM-dd")
        singleMethod_1 = ""
        singleMethod_2 = ""
        singleMethod_3 = ""
        singleMethod_4 = ""
        mainMethod = ""
        consumableName = ""
        consumableCode = ""
        operator = ""
        expCategories = ""
        expStep = 0
        expRemainTime = 0
        expRunningTime = 0
        expPauseTime = 0
        expWaitTime = 0
        consumableCodeStatus = false
        consumableCodePromptMsg = ""

        // 在这里加了一下刷新状态，要不然单模块的时候不刷新……
        pageExpMain.state2Edit()
        pageExp1.state2Edit()
        pageExp2.state2Edit()
        pageExp3.state2Edit()
        pageExp4.state2Edit()
    }

    function clearExpSelected(){
        singleMethod_1 = ""
        singleMethod_2 = ""
        singleMethod_3 = ""
        singleMethod_4 = ""
        mainMethod = ""
        consumableName = ""

        // 按照新版本的<清空>，不需要清空耗材编码
//        if(gPageIdx===0 || gPageIdx===4){
//            consumableCode = ""
//        }
    }

    function checkConsumableCode(code){     // 启动实验/点击回车时触发

        if(!isCheckConsumableCode){
            gStateExp.consumableCodeStatus = true
            return
        }

        var res = CBarCode.addBarCode(code)
        gStateExp.consumableCodePromptMsg = res
        if(res === ""){
            gStateExp.consumableCodeStatus = true
        }else{
            gStateExp.consumableCodeStatus = false
        }
    }

    function resetStatusAfterExperiment(){      // 实验结束后清空各种状态
        initExpInfo()
        clearModuleStatus()
        isConfirm_main = false
        isConfirm_1 = false
        isConfirm_2 = false
        isConfirm_3 = false
        isConfirm_4 = false
    }

    // 确认状态控制
    onMainMethodChanged: {

//        console.log("MainMethod变更为", mainMethod)

        isConfirm_main = false
        isConfirm_1 = false
        isConfirm_2 = false
        isConfirm_3 = false
        isConfirm_4 = false
        // 更新子实验状态
        if(mainMethod === ""){
            singleMethod_1 = ""
            singleMethod_2 = ""
            singleMethod_3 = ""
            singleMethod_4 = ""
            selectedExpType = -1
            selectedExpName = mainMethod
            resetExpInfo()
        }else{
            var singleMethodList = CGetMessageInterface.getSingleMethodByMainMethod(mainMethod)
            singleMethod_1 = singleMethodList[0]
            singleMethod_2 = singleMethodList[1]
            singleMethod_3 = singleMethodList[2]
            singleMethod_4 = singleMethodList[3]
            selectedExpType = 0
            selectedExpName = mainMethod
            resetExpInfo()
        }
        // 更新纯化耗材
        if(mainMethod === ""){
            consumableName = ""
        }else{
            consumableName = CGetMessageInterface.getConsumableByMainMethod(mainMethod)
        }
    }
    onSingleMethod_1Changed: {
        isConfirm_1 = false
        if(mainMethod === "" && singleMethod_1 !== ""){
            selectedExpType = 1
            selectedExpName = singleMethod_1
            resetExpInfo()
        }
    }onSingleMethod_2Changed: {
        isConfirm_2 = false
        if(mainMethod === "" && singleMethod_2 !== ""){
            selectedExpType = 2
            selectedExpName = singleMethod_2
            resetExpInfo()
        }
    }onSingleMethod_3Changed: {
        isConfirm_3 = false
        if(mainMethod === "" && singleMethod_3 !== ""){
            selectedExpType = 3
            selectedExpName = singleMethod_3
            resetExpInfo()
        }
    }onSingleMethod_4Changed: {
        isConfirm_4 = false
        if(mainMethod === "" && singleMethod_4 !== ""){
            selectedExpType = 4
            selectedExpName = singleMethod_4
            resetExpInfo()
        }
        else if(mainMethod === "" && singleMethod_4 === ""){
            selectedExpType = -1
            selectedExpName = ""
            resetExpInfo()
        }
        // 更新注射器颜色
        CAnimationPurify.updateColor(singleMethod_4)
    }

    Connections{
        target: mainwindow
        onGPageIdxChanged: {
            if(mainMethod !== ""){
                return
            }
            if(gPageIdx === 1){
                selectedExpName = singleMethod_1
                selectedExpType = selectedExpName==="" ? -1 : 1
                resetExpInfo()
            }if(gPageIdx === 2){
                selectedExpName = singleMethod_2
                selectedExpType = selectedExpName==="" ? -1 : 2
                resetExpInfo()
            }if(gPageIdx === 3){
                selectedExpName = singleMethod_3
                selectedExpType = selectedExpName==="" ? -1 : 3
                resetExpInfo()
            }if(gPageIdx === 4){
                selectedExpName = singleMethod_4
                selectedExpType = selectedExpName==="" ? -1 : 4
                resetExpInfo()
            }if(gPageIdx === 0){
                selectedExpName = ""
                selectedExpType = -1
                resetExpInfo()
            }
        }
    }

    function resetExpInfo(){
        CGetMessageInterface.setExpInfo(selectedExpType, selectedExpName, consumableName)
    }

    onConsumableNameChanged: {
        CAnimationPurify.setConsumableName(consumableName)
        CAnimationPurify.reset()
        resetExpInfo()
    }


}
